//
//  CZHomeOriginalView.swift
//  CZVVeiBo
//
//  Created by smile on 2017/8/2.
//  Copyright © 2017年 itcast. All rights reserved.
//

import UIKit
import SnapKit
// 原创微博
class CZHomeOriginalView: UIView {
    
    // 记录约束
    var bottomConstraint: Constraint?
    
    // 提供一个属性供外界赋值
    var homeViewModel: CZHomeViewModel?{
        didSet{
            // 赋值
            headImageView.cz_setImage(urlString: homeViewModel?.homeModel?.user?.profile_image_url, placeholderImgName: "avatar_default")
            nameLabel.text = homeViewModel?.homeModel?.user?.name
            contentLabel.attributedText = homeViewModel?.originalAttributedString
            membershipImageView.image = homeViewModel?.mbrankImage
            avatarImageView.image = homeViewModel?.verifiedImage
            /*
             pic_urls.count > 0
                - 代表有配图
                    - 配图赋值
                    - originalView.bottom = pictureView.bottom + 10
                    - 显示配图
             pic_urls.count <= 0 
                - 代表没有配图
                    - originalView.bottom = contentLabel.bottom + 10
                    - 隐藏配图
             */
            // 卸载
            bottomConstraint?.deactivate()
            // 有配图
//            homeViewModel?.homeModel
            if let pic_urls = homeViewModel?.homeModel?.pic_urls, pic_urls.count > 0 {
                // 赋值
                pictureView.picUrls = pic_urls
                // 约束
                snp.makeConstraints({ (make) in
                    bottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
                })
                // 显示
                pictureView.isHidden = false
                
            }else{
                // 没有配图
                // 约束
                snp.makeConstraints({ (make) in
                    bottomConstraint = make.bottom.equalTo(contentLabel).offset(10).constraint
                })
                // 隐藏配图
                pictureView.isHidden = true
            }
            
            // 处理微博来源
            sourceLabel.text = homeViewModel?.sinaSource
            // 微博时间
            timeLabel.text = homeViewModel?.sinaTimeStr
            
            
        }
    }
    
    

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    // MARK: - 设置视图
    private func setupUI(){
        backgroundColor = UIColor.white
        addSubview(headImageView)
        addSubview(nameLabel)
        addSubview(membershipImageView)
        addSubview(timeLabel)
        addSubview(sourceLabel)
        addSubview(avatarImageView)
        addSubview(contentLabel)
        addSubview(pictureView)
        
        headImageView.snp.makeConstraints { (make) in
            make.left.top.equalTo(10)
            make.size.equalTo(CGSize(width: 35, height: 35))
        }
        
        nameLabel.snp.makeConstraints { (make) in
            make.left.equalTo(headImageView.snp.right).offset(10)
            make.top.equalTo(headImageView)
        }
        
        membershipImageView.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel.snp.right).offset(10)
            make.centerY.equalTo(nameLabel)
        }
        
        timeLabel.snp.makeConstraints { (make) in
            make.left.equalTo(nameLabel)
            make.bottom.equalTo(headImageView)
        }
        
        sourceLabel.snp.makeConstraints { (make) in
            make.left.equalTo(timeLabel.snp.right).offset(10)
            make.bottom.equalTo(timeLabel)
        }
        
        avatarImageView.snp.makeConstraints { (make) in
            make.centerX.equalTo(headImageView.snp.right)
            make.centerY.equalTo(headImageView.snp.bottom)
        }
        
        contentLabel.snp.makeConstraints { (make) in
            make.left.equalTo(headImageView)
            make.top.equalTo(headImageView.snp.bottom).offset(10)
            make.width.equalTo(CZSCREENW - 20)
        }
        
        pictureView.snp.makeConstraints { (make) in
            make.left.equalTo(contentLabel)
            make.top.equalTo(contentLabel.snp.bottom).offset(10)
            make.size.equalTo(CGSize(width: 100, height: 100))
        }
        
        snp.makeConstraints { (make) in
            bottomConstraint = make.bottom.equalTo(pictureView).offset(10).constraint
        }
        
        // 测试数据
        timeLabel.text = "微博时间"

    }
    
    // MARK: - 懒加载控件
    // 头像
    private lazy var headImageView: UIImageView = UIImageView(imgName: "avatar_default")
    // 昵称
    private lazy var nameLabel: UILabel = {
        let lab = UILabel()
        lab.font = UIFont.systemFont(ofSize: 14)
        lab.textColor = UIColor.darkGray
        return lab
    }()
    // 会员等级
    private lazy var membershipImageView: UIImageView = UIImageView(imgName: "common_icon_membership")
    // 微博时间
    private lazy var timeLabel: UILabel = UILabel(textColor: CZAPPThemeColor, fontSize: 10)
    // 微博来源
    private lazy var sourceLabel: UILabel = UILabel(textColor: CZAPPThemeColor, fontSize: 10)

    // 会员认证
    private lazy var avatarImageView: UIImageView = UIImageView(imgName: "avatar_vgirl")
    // 微博文字
    private lazy var contentLabel: UILabel = {
        let lab =  UILabel(textColor: UIColor.darkGray, fontSize: 14)
        lab.numberOfLines = 0
        return lab
    }()
    // 配图
    private lazy var pictureView: CZHomePictureView = {
        let view = CZHomePictureView()
        view.backgroundColor = self.backgroundColor
        return view
    }()

}
